package cn.parus.common.Execption;

import lombok.Data;
import java.util.HashMap;
import java.util.Map;

@Data
public class Result<T> {
    private Integer code;
    private String msg;
    private T data;
    private Map<String, Object> extra = new HashMap<>();

    public static <T> Result<T> success(T data) {
        Result<T> result = new Result<>();
        result.setCode(ResultCode.SUCCESS.getCode());
        result.setMsg(ResultCode.SUCCESS.getMsg());
        result.setData(data);
        return result;
    }

    public static <T> Result<T> error(String message) {
        Result<T> result = new Result<>();
        result.setCode(ResultCode.SERVER_ERROR.getCode());
        result.setMsg(message);
        return result;
    }

    public Result<T> addExtra(String key, Object value) {
        this.extra.put(key, value);
        return this;
    }

    public enum ResultCode {
        SUCCESS(200, "操作成功"),
        BAD_REQUEST(400, "参数错误"),
        UNAUTHORIZED(401, "未授权"),
        FORBIDDEN(403, "禁止访问"),
        NOT_FOUND(404, "资源未找到"),
        SERVER_ERROR(500, "服务器错误");

        private final int code;
        private final String msg;

        ResultCode(int code, String msg) {
            this.code = code;
            this.msg = msg;
        }

        public int getCode() { return code; }
        public String getMsg() { return msg; }
    }
}
